<?php

namespace App\Models\Rbac;

use App\Models\Common\CommonModel;
use Illuminate\Support\Arr;

class Permission extends CommonModel {

    protected $table = 'guard_permission';

    public function moreChildren() {
        return $this->hasMany(Permission::class,'pid','id')->orderBy('sort','desc');
    }

    public function children() {
        return $this->moreChildren()->with(['children']);
    }

    public function getRecords(array $conditions) {
        $records = parent::getRecords($conditions);
        if(isset($conditions['pid'])){
            $records = $records->where('pid',$conditions['pid']);
        }
        return $records;
    }

    public static function getMaxSort() {
        return self::selectRaw('MAX(sort) as value')->get()->first()->value??0;
    }




}
